﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;

namespace FMPCore
{
    public class Contact : INotifyPropertyChanged
    {
        /// <summary>
        /// The differnet statuses a user can be in
        /// </summary>
        public enum UserStatus
        {
            Online = 1,
            Offline = 0
        }

        /// <summary>
        /// Defualt constructor, set the status of the user to Offline
        /// </summary>
        public Contact()
        {
            Status = UserStatus.Offline;
        }

        private string m_userName;
        /// <summary>
        /// The name of the user
        /// </summary>
        public string UserName
        {
            get
            {
                return m_userName;
            }
            set
            {
                m_userName = value;
                onPropertyChanged(o => o.UserName);
            }
        }

        private UserStatus m_status;
        /// <summary>
        /// The status of the user
        /// </summary>
        public UserStatus Status
        {
            get
            {
                return m_status;
            }
            set
            {
                m_status = value;
                onPropertyChanged(o => o.Status);
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        /*
         * Found how to do this at: http://blogs.microsoft.co.il/blogs/dorony/archive/2007/08/31/WPF-Binding_2C00_-INotifyPropertyChanged-and-Linq.aspx
         */
        /// <summary>
        /// Access the event to trigger the PropertyChanged event
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <param name="propertyExpr">The property</param>
        protected virtual void onPropertyChanged<R>(Expression<Func<Contact, R>> propertyExpr)
        {
            onPropertyChanged(this.GetPropertySymbol(propertyExpr));
        }

        /// <summary>
        /// Access the event to trigger the PropertyChanged event
        /// </summary>
        /// <param name="propertyName">Name of the property that changed</param>
        private void onPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }

    /*
     * Found how to do this at: http://blogs.microsoft.co.il/blogs/dorony/archive/2007/08/31/WPF-Binding_2C00_-INotifyPropertyChanged-and-Linq.aspx
     */
    /// <summary>
    /// A class that has an extention method
    /// </summary>
    public static class SymbolExtensions 
    { 
        /// <summary>
        /// Gets the memeber name
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="obj"></param>
        /// <param name="expr"></param>
        /// <returns>Name of the memeber</returns>
        public static string GetPropertySymbol<T, R>(this T obj, Expression<Func<T, R>> expr) 
        { 
            return ((MemberExpression)expr.Body).Member.Name;
        } 
    }
}
